﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Elsa.CSharp.Activities;
using Elsa.JavaScript.Activities;
using Elsa.Workflows;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Memory;
using T.FAS.Runtime.MF.Activities;
using T.FAS.Runtime.MF.Entity;

namespace T.FAS.Runtime.MF.Activities.Helper
{
    public class ActivityHelper
    {
        public static string GetActivityPort(IActivity activity, PortItem item)
        {
            if (activity is Start || activity is FasStartActivity)
            {
                if (item.Group.ToLower() == "in")
                {
                    return "In";
                }
                else if (item.Group.ToLower() == "out")
                {
                    return "Done";
                }
            }
            else if (activity is End endActivity || activity is FasEndActivity)
            {
                if (item.Group.ToLower() == "in")
                {
                    return "In";
                }
                else if (item.Group.ToLower() == "out")
                {
                    return "Done";
                }
            }
            else if (activity is End codeActivity || activity is RunJavaScript || activity is RunCSharp || activity is JavaScriptActivity)
            {
                if (item.Group.ToLower() == "in")
                {
                    return "In";
                }
                else if (item.Group.ToLower() == "out")
                {
                    return "Done";
                }
            }
            else if (activity is ConditionBranchActivity)
            {
                if (item.Group.ToLower() == "in")
                {
                    return "In";
                }
                else if (item.Group.ToLower() == "out")
                {
                    return item.Id;
                }
            }
            else if (activity is CDOActivity)
            {
                if (item.Group.ToLower() == "in")
                {
                    return "In";
                }
                else if (item.Group.ToLower() == "out")
                {
                    return item.Id;
                }
            }
            else if (activity is LLMActivity)
            {
                if (item.Group.ToLower() == "in")
                {
                    return "In";
                }
                else if (item.Group.ToLower() == "out")
                {
                    return "Done";
                }
            }
            else if (activity is FasIterationActivity || activity is ForEach || activity is ParallelForEach)
            {
                if (item.Group.ToLower() == "in")
                {
                    return "In";
                }
                else if (item.Group.ToLower() == "out")
                {
                    return "Done";
                }
            }
            else if (activity is DataQueryActivity)
            {
                if (item.Group.ToLower() == "in")
                {
                    return "In";
                }
                else if (item.Group.ToLower() == "out")
                {
                    return "Done";
                }
            }
            else if (activity is ActionExecuteActivity)
            {
                if (item.Group.ToLower() == "in")
                {
                    return "In";
                }
                else if (item.Group.ToLower() == "out")
                {
                    return "Done";
                }
            }
            return "";
        }
        public static string GetSourceVariableRealName(ShapeBase shape, string variableName, string nodeId)
        {
            var varSelectors = variableName.Split('.');
            var varItem = varSelectors[0];

            //从整个工作流中获取应的变量信息
            // var varInfo = (shape.BelongTo.NodeInputOutputVariables ?? new List<UserVariable>()).Find(t => t.VariableName == varItem && t.ActivityID == nodeId);
            var varInfo = GetSourceVariable(shape, varItem, nodeId);
            if (varInfo == null) return variableName;
            var varName = varInfo.CompositeVarName;
            varSelectors[0] = varName;
            variableName = string.Join(".", varSelectors);
            return variableName;
        }
        public static UserVariable GetSourceVariable(ShapeBase shape, string variableName, string nodeId)
        {
            //从整个工作流中获取应的变量信息
            var varInfo = (shape.BelongTo.NodeInputOutputVariables ?? new List<UserVariable>()).Find(t => t.VariableName == variableName && t.ActivityID == nodeId);
            return varInfo;
        }

        public static string GetVariableExpression(string variableName,string dataType,bool onlyCreateDef=true)
        {
            var expression = string.Empty;

            var varSelectors = variableName.Split('.');
            var varItem = varSelectors[0];
            /*/if (varSelectors.Length > 1 || varSelectors.Count() == 1 && (dataType.ToLower() == "record" || dataType.ToLower() == "record"))
            {
                expression = $"let {varItem} = JSON.parse(JSON.stringify(variables.{varItem}));";
                
            }
            else
            {
                expression = $"let {varItem} = variables.{varItem};";
            }*/

            expression = $"let {varItem} = variables.{varItem};";
            if (onlyCreateDef==false)
            {
                expression += $"{variableName};";
            }
            return expression;
        }
    }
}
